package com.zhongya666.library.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import com.zhongya666.library.R
import com.zhongya666.library.databinding.WanDialogBasicBinding
import com.zhongya666.library.util.ScreenUtil

/**
 * Github/Gitee：zhongya666 2021/7/28 16:51
 */

/**
 * 设置
 * 宽度 1 - 10 默认是8
 * 高度自适应
 */
class BasicDialog(private val mContext: Context,var dialogWidth : Int = 8) : Dialog(
    mContext, R.style.dialog_basic_style
) {
    private var binding: WanDialogBasicBinding = WanDialogBasicBinding.inflate(layoutInflater)
    private var radius : Float = 22f

    private var actionWhich: ((Int) -> Unit)? = null //0取消 1确定
    private var action: (() -> Unit)? = null //只响应确定事件

    init {
        initView()
    }

    fun setAction(action : () -> Unit){
        this.action = action
    }

    fun setActionWhich(actionWhich : (Int) -> Unit){
        this.actionWhich = actionWhich
    }

    private fun initView() {
        

        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setScreenWidth(dialogWidth)
        setContentView(binding.root)

        binding.tvCancel.setOnClickListener {
            actionWhich?.invoke(0)
            dismiss()
        }
        binding.tvAgree.setOnClickListener {
            action?.invoke()
            actionWhich?.invoke(1)
            dismiss()
        }
    }

    /**
     * title 不传的话就会隐藏
     */

    fun setContent( content: String, title: String? = null,agreeText: String = "确定", cancelText: String = "取消") {

        if (title == null) {
            binding.tvTitle.visibility = View.GONE
        }else{
            binding.tvTitle.text = title
        }

        binding.tvContent.text = content
        binding.tvAgree.text = agreeText
        binding.tvCancel.text = cancelText
    }

    /**
     * spanContent SpannableString
     */
    fun setSpanContent(spanContent: SpannableString, title: String? = null, agreeText: String = "确定", cancelText: String = "取消") {

        if (title == null) {
            binding.tvTitle.visibility = View.GONE
        }else{
            binding.tvTitle.text = title
        }

        binding.tvContent.text = spanContent
        binding.tvAgree.text = agreeText
        binding.tvCancel.text = cancelText

        binding.tvContent.highlightColor = Color.TRANSPARENT //设置点击后的颜色为透明，否则会一直出现高亮
        binding.tvContent.movementMethod = LinkMovementMethod.getInstance() //开始响应点击事件
    }


    fun setHtmlContent( htmlContent: CharSequence,title: String? = null) {

        if (title == null) {
            binding.tvTitle.visibility = View.GONE
        }else{
            binding.tvTitle.text = title
        }

        binding.tvContent.text = htmlContent

    }

    private fun setScreenWidth(int: Int) {
        window?.decorView?.setPadding(0, 0, 0, 0)
        val lp = window?.attributes!!
        window?.attributes?.width = ScreenUtil.getScreenWidth(context) * int / 10
        window?.attributes?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window?.attributes = lp
    }

    /**
     * 隐藏取消按钮取消
     */
    fun goneCancel() {
        binding.tvCancel.visibility = View.GONE
        binding.viewLine2.visibility = View.GONE
    }

    /**
     * 设置确定 取消按钮大小 单位sp
     */
    fun setAgreeTextSize(float: Float) {
        binding.tvAgree.setTextSize(TypedValue.COMPLEX_UNIT_SP,float)
    }

    fun setCancelTextSize(float: Float) {
        binding.tvCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP,float)
    }

    fun setContentTextSize(float: Float) {
        binding.tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP,float)
    }

    fun setTitleTextSize(float: Float) {
        binding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,float)
    }

    /**
     * 设置字体颜色
     * 默认#333
     * 确定按钮默认@android:color/holo_blue_light
     */

    fun setAgreeTextColor(color: Int) {
        binding.tvAgree.setTextColor(color)
    }

    fun setCancelTextColor(color: Int) {
        binding.tvCancel.setTextColor(color)
    }

    fun setContentTextColor(color: Int) {
        binding.tvContent.setTextColor(color)
    }

    fun setTitleTextColor(color: Int) {
        binding.tvTitle.setTextColor(color)
    }

    /**
     * 设置确定按钮的背景颜色
     */
    fun setAgreeBackgroundColor(color: Int) {
        val backgroundDrawable = GradientDrawable()
        var a : FloatArray = floatArrayOf(0f,0f,0f,0f,radius,radius,0f,0f)
        if (binding.tvCancel.visibility == View.GONE){
            a = floatArrayOf(0f,0f,0f,0f,radius,radius,radius,radius)
        }
        backgroundDrawable.cornerRadii = a
        backgroundDrawable.setColor(color)
        binding.tvAgree.background = backgroundDrawable
    }

    /**
     * 设置弹框背景
     */
    fun setDialogBackgroundColor(color: Int) {
        binding.llRoot.setBackgroundColor(color)
    }

    fun setDialogBackgroundResource(resource: Int) {
        binding.llRoot.setBackgroundResource(resource)
    }

    /**
     * 设置分割线颜色
     */
    fun setDividingLineColor (color: Int) {
        binding.viewLine1.setBackgroundColor(color)
        binding.viewLine2.setBackgroundColor(color)
    }

    /**
     * 修改弹框背景颜色和圆角
     */
    fun setBackgroundColorAndRadius(color: Int,radius : Float = 22f) {
        this.radius = radius
        val backgroundDrawable = GradientDrawable()
        backgroundDrawable.cornerRadius = radius
        backgroundDrawable.setColor(color)
        binding.llRoot.background = backgroundDrawable
    }

}